package com.netposa.datawarehouse.aggregator.service.impl;

import com.netposa.appwebres.exception.WebResException;
import com.netposa.datawarehouse.aggregator.service.AggregatorCommonService;
import com.netposa.imageserver.client.ImageServerClient;
import com.netposa.imageserver.client.common.ImageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

@Service
public class AggregatorCommonServiceImpl implements AggregatorCommonService {
   private static final Logger log = LoggerFactory.getLogger(AggregatorCommonServiceImpl.class);
   @Autowired
   private ImageServerClient client;

   public String imgToBase64(String path) {
      if (StringUtils.isBlank(path)) {
         throw new WebResException("499", new Object[]{"图片地址为空"});
      } else {
         try {
            ImageInfo info = this.client.downloadImage(path);
            if (null != info && null != info.getData()) {
               byte[] image = info.getData();
               String base64 = Base64Utils.encodeToString(image);
               return base64;
            } else {
               log.error("handle image error:图片信息不存在");
               throw new WebResException("499", new Object[]{"图片处理失败"});
            }
         } catch (Exception var5) {
            log.error("handle image error:{}", var5.getMessage());
            var5.printStackTrace();
            throw new WebResException("499", new Object[]{"图片处理失败"});
         }
      }
   }
}
